package com.wj.console.common.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wj.console.constant.Constant;
import com.base.util.FileDESUtil;
import com.base.util.UUIDUtils;

@Service
public class FileServerService {
	private final static Logger log= Logger.getLogger(FileServerService.class);
	@Autowired
	private Constant constant;
	/**
	 * 上传文件到文件服务器
	 * @param orgId
	 * @param file
	 * @param path
	 * @return   服务器相对文件路径
	 */
	public String upload(File file, String orgId, String path) {
		String toFileName = UUIDUtils.getUUIDStr() + getSuffix(file.getName());
		return upload(file, orgId, path, toFileName);
	}
	/**
	 * 上传文件到文件服务器
	 * @param file
	 * @param orgId
	 * @param path
	 * @param fileName
	 * @return  服务器相对文件路径
	 */
	public String upload(File file, String orgId, String path, String fileName){
		String fileServerRoot = constant.getFileUploadPath();
		orgId = StringUtils.isEmpty(orgId) ? "" : File.separator + orgId;
		String toPath = String.format("%s%s%s%s", Constant.ROOT_PATH, orgId, path, getDateDir());
	    String toFileName = fileName;
		File toDir = new File(fileServerRoot + toPath);
		if(!toDir.exists()){
			toDir.mkdirs();
		}
		FileInputStream ins = null;
		FileOutputStream os = null;
		toFileName = toPath + File.separator + toFileName;
		try {
			ins = new FileInputStream(file);
			os = new FileOutputStream(fileServerRoot + toFileName);
			inputstreamToFile(ins, os);
		} catch (FileNotFoundException e) {
			log.error("文件未找到", e);
		}finally{
			  if(os != null){
				  try {
						os.close();
					} catch (IOException e) {
					}
			  }
			  if(ins != null){
				  try {
					  ins.close();
					} catch (IOException e) {
						
					}
			  }
			  
		  }
		
		return toFileName;
	}
	
	public String uploadByEncrypt(InputStream ins, String orgId, String path, String fileName){
		String fileServerRoot = constant.getFileUploadPath();
		orgId = StringUtils.isEmpty(orgId) ? "" : File.separator + orgId;
		String toPath = String.format("%s%s%s%s", Constant.ROOT_PATH, orgId, path, getDateDir());
	    String toFileName = fileName;
		File toDir = new File(fileServerRoot + toPath);
		if(!toDir.exists()){
			toDir.mkdirs();
		}
		FileOutputStream os = null;
		toFileName = toPath + File.separator + toFileName;
		try {
			os = new FileOutputStream(fileServerRoot + toFileName);
			FileDESUtil.encrypt(ins, os);
		} catch (Exception e) {
			log.error("上传文件错误", e);
		}finally{
			  if(os != null){
				  try {
						os.close();
					} catch (IOException e) {
					}
			  }
		  }
		
		return toFileName;
	}
	
	/**
	 * 上传文件到文件服务器
	 * @param ins
	 * @param orgId
	 * @param path
	 * @param fileName
	 * @return  服务器相对文件路径
	 */
	public String upload(InputStream ins, String orgId, String path, String fileName){
		String fileServerRoot = constant.getFileUploadPath();
		orgId = StringUtils.isEmpty(orgId) ? "" : File.separator + orgId;
		String toPath = String.format("%s%s%s%s", Constant.ROOT_PATH, orgId, path, getDateDir());
	    String toFileName = fileName;
		File toDir = new File(fileServerRoot + toPath);
		if(!toDir.exists()){
			toDir.mkdirs();
		}
		FileOutputStream os = null;
		toFileName = toPath + File.separator + toFileName;
		try {
			os = new FileOutputStream(fileServerRoot + toFileName);
			inputstreamToFile(ins, os);
		} catch (FileNotFoundException e) {
			log.error("文件未找到", e);
		}finally{
			  if(os != null){
				  try {
						os.close();
					} catch (IOException e) {
					}
			  }
		  }
		
		return toFileName;
	}
	
	private void inputstreamToFile(InputStream ins, FileOutputStream os) {
		  try {
			   int bytesRead = 0;
			   byte[] buffer = new byte[8192];
			   while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
			        os.write(buffer, 0, bytesRead);
			   }
			   
		  } catch (Exception e) {
		       log.error("文件输出失败", e);
		  }
	}
	
	private String getSuffix(String name){
		if(StringUtils.isEmpty(name)){
			return "";
		}
		return name.substring(name.lastIndexOf("."));
	}
	
	private String getDateDir() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		return File.separator + sdf.format(new Date());
	}
}
